package com.itheima.health.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.PermissionService;
import com.itheima.health.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    RoleService roleService;
    @Reference
    PermissionService permissionService;

    @GetMapping("findPage")
    public Result findPage(QueryPageBean queryPageBean) {

        //rpc查询结果
        PageResult pageResult = roleService.pageQuery(queryPageBean);
        //构造返回对象
        return new Result(true,  MessageConst.ACTION_SUCCESS, pageResult);

    }


    @PostMapping("/add")
    public Result add(@RequestBody Role role, Integer[] permissionIds, Integer[] menuIds) {
        //RPC请求添加
        roleService.add(role, permissionIds, menuIds);
        return new Result(true, MessageConst.ACTION_SUCCESS);
    }

    @PostMapping("/edit")
    public Result edit(@RequestBody Role role, Integer[] permissionIds, Integer[] menuIds) {
        //RPC请求添加
        roleService.edit(role, permissionIds, menuIds);
        return new Result(true,  MessageConst.ACTION_SUCCESS);

    }


    @RequestMapping("/findById")

    public Result findById(Integer id) {
        log.info("[检查项-根据ID查询]id:{}", id);
        Role role = roleService.findById(id);
        return new Result(true, MessageConst.ACTION_SUCCESS, role);
    }


    @RequestMapping("/delete")
    public Result delete(Integer id) {
        log.info("[检查项-根据id删除]id：{}", id);
        //RPC调用处理业务
        roleService.delete(id);
        return new Result(true,  MessageConst.ACTION_SUCCESS);
    }

    @GetMapping("/findMenuIdsByRoleId")
    public Result findRoleIdsByMenuId(Integer id) {
        List<Integer> itemIds = roleService.findRoleIdsByMenuId(id);
        //封装返回值
        return new Result(true, MessageConst.ACTION_SUCCESS, itemIds);
    }

    @GetMapping("/findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(Integer id) {
        List<Integer> itemIds = permissionService.findPermissionIdsByRoleId(id);
        //封装返回值
        return new Result(true, MessageConst.ACTION_SUCCESS, itemIds);
    }



}

